package com.hjy.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.hjy.common.constants.JwtConstants;
import com.hjy.common.constants.TokenConstants;
import com.hjy.common.result.Result;
import com.hjy.common.result.User;
import com.hjy.common.utils.JwtUtils;
import com.hjy.common.utils.TelSmsUtils;
import com.hjy.remote.SysUserRemote;
import com.hjy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Pattern;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserRemote sysUserRemote;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TelSmsUtils telSmsUtils;
    /**
     * 发送手机号
     */
    @Override
    public Result<User> send(String tel) {
        //判断手机号格式是否正确
        Pattern compile = Pattern.compile("^(?:(?:\\+|00)86)?1[3-9]\\d{9}$");
        if (!compile.matcher(tel).matches()) {
            return Result.error("手机号格式错误！");
        }
        //判断手机号是否存在
        Result<User> send = sysUserRemote.send(tel);
        if (null==send){
            return Result.error("手机号不存在！");
        }
        //存验证码
        String code = RandomUtil.randomNumbers(4);
        log.info("验证码为：{}",code);
        stringRedisTemplate.opsForValue().set(tel, code);
        //发送手机号
//        telSmsUtils.sendSms(tel,new HashMap<String, String>(){{put("code", code);}});

        return send;
    }
    /**
     * 登录
     */
    @Override
    public Result<User> login(User user) {
        //判断 手机号是否存在
        Result<User> login = sysUserRemote.login(user);
        User data = login.getData();
        if (null==data){
            return Result.error("手机号不存在");
        }
        //判断验证码是否正确
        String codes = stringRedisTemplate.opsForValue().get(user.getTel());
        if (!codes.equals(user.getCode())){
            return Result.error("验证码错误");
        }
        //生成唯一标识
        String string = UUID.randomUUID().toString();
        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtConstants.USER_KEY, string);
        //生成token
        String token = JwtUtils.createToken(map);
        //存token
        stringRedisTemplate.opsForValue().set(TokenConstants.TOKEN+string, JSONObject.toJSONString(data));
        //返回
        User user1 = new User();
        user1.setToken(token);
        return Result.success(user1);
    }
    /**
     * 获取登录人信息
     */
    @Override
    public User getUserInfo() {
        String token = request.getHeader(TokenConstants.TOKEN);

        String userKey = JwtUtils.getUserKey(token);

        String s = stringRedisTemplate.opsForValue().get(TokenConstants.TOKEN + userKey);

        return JSONObject.parseObject(s, User.class);
    }
}
